/**
 * 文件：ModifyCoursePanel.java
 * 说明：用于修改课程
 * 作者：古通基
 * 时间：1211-09-13
 */

package com.java.component;

import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class ModifyCoursePanel extends JPanel implements ActionListener,MouseListener
{
	private static final long serialVersionUID = 4765604146270672230L;
	private JLabel lno = new JLabel("课  程  号：");                           //静态标签
	private JLabel lname = new JLabel("课  程  名：");
	private JLabel lteacher = new JLabel("授课老师：");              
	private JLabel lcredit = new JLabel("学        分：");        
	private JLabel ltime = new JLabel("课        时：");     
	private JLabel laddress = new JLabel("上课地点：");         
	
	private JTextField no = new JTextField("请输入课程号",10);          //文本输入
	private JTextField name = new JTextField("",10); 
	private JTextField teacher = new JTextField("",10); 
    String[]  credits = {"1","2","3","4","5"};
	private JComboBox credit= new JComboBox(credits);  
	String[]  times = {"16","32","48","64","72"};
	private JComboBox time= new JComboBox(times);  
    String[]  addresses = {"二区","三区","四区","五区"};
    private JComboBox address = new JComboBox(addresses);       
    
    private JButton query = new JButton("查询");                             //声明按钮
    private JButton ok = new JButton("确定");
    private JButton modify = new JButton("修改");
    	
    private JPanel JPcomon1 = new JPanel();                                    //声明面板
    private JPanel JPcomon2 = new JPanel();
    private JPanel JPcomon3 = new JPanel();
    private JPanel JPcomon4 = new JPanel();
    private JPanel JPcomon5 = new JPanel();
    private JPanel JPcomon6 = new JPanel();
    private JPanel JPbutton = new JPanel();
    
    public ModifyCoursePanel()                                                        //构造函数
    {
    	lno.setHorizontalAlignment(SwingConstants.CENTER);
    	lname.setHorizontalAlignment(SwingConstants.CENTER);
    	lteacher.setHorizontalAlignment(SwingConstants.CENTER);
    	lcredit.setHorizontalAlignment(SwingConstants.CENTER);
    	ltime.setHorizontalAlignment(SwingConstants.CENTER);
    	laddress.setHorizontalAlignment(SwingConstants.CENTER);	
    	
    	JPcomon1.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon1.add(lno);
    	JPcomon1.add(no);
    	JPcomon1.add(query);
    	JPcomon2.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon2.add(lname);
    	JPcomon2.add(name);
    	JPcomon3.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon3.add(lteacher);
    	JPcomon3.add(teacher);
    	JPcomon4.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon4.add(lcredit);
    	JPcomon4.add(credit);
    	JPcomon5.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon5.add(ltime);
    	JPcomon5.add(time);
    	JPcomon6.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon6.add(laddress);
    	JPcomon6.add(address);    	
    	JPbutton.setLayout(new FlowLayout(FlowLayout.CENTER,12,5));
    	JPbutton.add(modify);
    	JPbutton.add(ok);
    	
    	this.setLayout(new GridLayout(10,1));
    	this.add(JPcomon1);
    	this.add(JPcomon2);
    	this.add(JPcomon3);
    	this.add(JPcomon4);
    	this.add(JPcomon5);
    	this.add(JPcomon6);
    	this.add(JPbutton);
    	
    	query.addActionListener(this);   
    	ok.addActionListener(this);                                      //事件监听        
    	modify.addActionListener(this);
    	no.addMouseListener(this);	
    }
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().equals("查询"))
		{
			QueryDataBase();
			no.setEditable(false);
			name.setEditable(false);
			teacher.setEditable(false);
			credit.setEnabled(false);
			time.setEnabled(false);
			address.setEnabled(false);
			query.setEnabled(false);
		}
		else if(e.getActionCommand().equals("修改"))
		{
			no.setEditable(true);
			name.setEditable(true);
			teacher.setEditable(true);
			credit.setEnabled(true);
			time.setEnabled(true);
			address.setEnabled(true);  
			query.setEnabled(true);
		}
		else if(e.getActionCommand().equals("确定"))
		{
			Ok();
		}
	}		
	
	private void QueryDataBase()
	{
		ResultSet rs = null;
		try
		{
			BaseSQL course = null;
			course = new BaseSQL();
			String sql = "select * from course where cno = '"+Integer.parseInt(no.getText())+"'";  
			
		    rs = course.executeSelect(sql);
		    if(rs.next())
    		{
    			if(rs.getInt("cno")==Integer.parseInt(no.getText()))
    			{
    				name.setText(rs.getString("cname"));
    				teacher.setText(rs.getString("teacher"));
    				credit.setSelectedItem(rs.getString("credit"));
    				time.setSelectedItem(rs.getString("time"));
    				address.setSelectedItem(rs.getString("address"));			
        		}    				
       		} 
    		else
    		{
    			JOptionPane.showMessageDialog(null,"不存在课号为："+no.getText()+" 的课程信息","查询错误",JOptionPane.INFORMATION_MESSAGE);
    		}
		}
		catch(SQLException evt)
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		} 
		catch (ClassNotFoundException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
		catch (NumberFormatException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
	}

	private void Ok()
	{
		try
		{
			BaseSQL course = null;
			course = new BaseSQL();
			String sql = "update course" +
					" set cname='"+name.getText()+"',teacher='"+teacher.getText()+"',credit= '"+Integer.parseInt((String) credit.getSelectedItem())+"',time='"+Integer.parseInt((String) time.getSelectedItem())+"',address='"+ address.getSelectedItem()+"' where cno = '"+Integer.parseInt(no.getText())+"'";
			course.executeDML(sql);
		    JOptionPane.showMessageDialog(this, "修改成功", "修改课程信息",JOptionPane.INFORMATION_MESSAGE);
		}
		catch(SQLException evt)
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		} 
		catch (ClassNotFoundException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
		catch (NumberFormatException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
	}	
	
    public void mouseClicked(MouseEvent e) { }                                //鼠标事件
    public void mouseReleased(MouseEvent e) { }
    public void mouseEntered(MouseEvent e) 
    {
    	if(no.getText().equals("请输入课程号"))
    	{    		
    		no.setText("");
    		this.setVisible(true);
    	}    	
    }
    public void mousePressed(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }
}

